﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BookExchange.Business;
using BookExchange.CustomClass.Base;
using BookExchange.ODataService;
using BookExchange.ViewModel;

namespace BookExchange
{
    public partial class MemberDetail : BasePage
    {
        MemberBusiness memberBus;

        public MemberDetail():base()
        {
            memberBus = new MemberBusiness();
        }

        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!IsPostBack)
            {
                //Get current member
                var currentMem = memberBus.GetMemberByCode(CurrentMember.MemberCode);

                //Bind to GUI
                BindData(currentMem);
            }
        }

        private void BindData(Member member)
        {
            if (member == null)
            {
                userInformationArea.Visible = false;
                return;
            }

            userInformationArea.Visible = true;
            txtDisplayName.Value = member.DisplayName;
            txtEmail.Value = member.Email;
            txtFullName.Value = member.DisplayName;
            //txtBirthday.Value = DateTime.Now.ToString(AppConfiguration.Instance.DateFormat);
            txtAddress.Value = member.Address;
            txtMobile.Text = member.Mobile == null ? string.Empty : member.Mobile.Value.ToString();
            txtCreateDate.InnerText = GetDate(member.CreatingDate);
            txtLastUpdated.InnerText = GetDate(member.LastModified);

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            //Update data from GUI
            var newMember = memberBus.GetMemberByCode(CurrentMember.MemberCode);
            newMember = UpdateNewMemberInfoFromGui(newMember);

            //Update to database
            memberBus.UpdateMember(newMember);            
        }

        private Member UpdateNewMemberInfoFromGui(Member mem)
        {
            mem.Facebook = " ";
            mem.DisplayName = txtDisplayName.Value;
            mem.Address = txtAddress.Value;
            mem.Mobile = int.Parse(txtMobile.Text);
            

            return mem;
        }

        protected void btnUpdatePassword_Click(object sender, EventArgs e)
        {
            //Get values from GUI
            var oldPassword = txtOldPassword.Value.Trim();
            var newPassword = txtNewPassword.Value.Trim();

            //Validate input before save
            if (string.IsNullOrEmpty(oldPassword) || string.IsNullOrEmpty(newPassword) || oldPassword == newPassword)
            {
                return;
            }

            //Update new password and save
            var currentMem = memberBus.GetMemberByCode(CurrentMember.MemberCode);
            if (currentMem == null)
            {
                Response.Redirect("default.aspx");                
            }
            else
            {
                currentMem.Password = txtNewPassword.Value;
                memberBus.UpdateMember(currentMem);
            }
        }

    }
}